package org.alvin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 国际化内容重复问题
 */
public class JsonIsRepeat {

    public static void main(String[] args) throws IOException {
//        加载所有的文件
        String dir = "F:\\tangzhichao\\qms-view\\src\\locales\\en";
        Map<String, Object> map = Maps.newHashMap();
        Map<String, Path> keyPathMap = Maps.newHashMap();
        Files.list(Paths.get(dir)).filter(item ->
                !item.equals("index.ts")
        ).forEach(item -> {
            try {
                String content = new String(Files.readAllBytes(item));
                String regex = "(\\w+) = ([{]([\\w\\W]+)[}])";
                Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
                Matcher m = p.matcher(content);

                if (m.find()) {
//                    System.out.println(111 + "==" + m.group(1));
//                   System.out.println(222+"==="+m.group(2));
                    JSONObject jsonObject = JSON.parseObject(m.group(2));
//                    System.out.println("json===" + jsonObject.toJSONString());
                    map.putAll(jsonObject);
                    for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                        keyPathMap.put(entry.getKey(), item);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        Map<String, List<String>> repeatMap = Maps.newHashMap();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            if (!repeatMap.containsKey(entry.getValue().toString())) {
                repeatMap.put(entry.getValue().toString(), Lists.newArrayList());
            }
            repeatMap.get(entry.getValue().toString()).add(entry.getKey());
        }
        //输出重复的可以：
        System.out.println("值重复的key");
        List<Rank> ranks = Lists.newArrayList();
        for (Map.Entry<String, List<String>> entry : repeatMap.entrySet()) {
            if (entry.getValue().size() == 1) {
                continue;
            }
            System.out.println("同一个值：" + entry.getKey() + "  重复的次数:" + entry.getValue().size());
            ranks.add(new Rank(entry.getKey(), entry.getValue().size()));
            for (String str : entry.getValue()) {
                System.out.println(str + "==" + keyPathMap.get(str));
            }
        }
        //
        Collections.sort(ranks);
        System.out.println("出现次数");
        for (Rank r :ranks) {
            System.out.println(r.name + "----" + r.rank);
        }
    }


   static class Rank implements Comparable {

        String name;
        Integer rank;

        public Rank(String name,int rank){
            this.name = name;
            this.rank = rank;
        }

        @Override
        public int compareTo(Object o) {
            Rank r = (Rank) o;
            int c = rank.compareTo(r.rank);
            return c == 0 ? name.compareTo(r.name) : c;
        }
    }
}
